package com.anjho.bean;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.eclipse.persistence.exceptions.DatabaseException;
import org.primefaces.event.SelectEvent;

import com.anjho.dao.DAOProduct;
import com.anjho.dao.DAOReserve;
import com.anjho.exceptions.InvalidGenreException;
import com.anjho.pojo.Client;
import com.anjho.pojo.Product;
import com.anjho.pojo.Reserve;

@ManagedBean(name = "reserveBean")
@ViewScoped
public class ReserveBean extends GenericBean {

	private static final long serialVersionUID = -462607283491688642L;
	
	private Client client;
	private Product product;
	private Product reservedProduct;
	private List<Reserve> reserveList;
	private List<Product> reservedProductList;
	private List<Product> productList;

	public ReserveBean() {
		super();
		this.clearForm();
	}
	
	public void clearForm(){
		this.reserveList = new ArrayList<Reserve>();
		this.reservedProductList = new ArrayList<Product>();
		this.productList = new ArrayList<Product>();
		this.product = new Product();
		this.client = new Client();
	}
	
	public void setReservedProductList(List<Product> reservedProductList) {
		this.reservedProductList = reservedProductList;
	}

	public void setProductList(List<Product> productList) {
		this.productList = productList;
	}

	public Client getClient() {
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
	}
	
	public List<Reserve> getReserveList() {
		return reserveList;
	}

	public void setReserveList(List<Reserve> reserveList) {
		this.reserveList = reserveList;
	}
	
	public List<Product> getProductList() {
		synchronized (this) {
			DAOProduct daoProduct = new DAOProduct();
			try {
				productList = daoProduct.getProductList();
			} catch (Exception e) {
				FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, "Erro", e.getMessage());
				FacesContext.getCurrentInstance().addMessage(null, facesMsg);
			}
			return productList;
		}
	}

	public List<Product> getReservedProductList() {
		this.acClientSelect(null);
		return reservedProductList;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public Product getReservedProduct() {
		return reservedProduct;
	}

	public void setReservedProduct(Product reservedProduct) {
		this.reservedProduct = reservedProduct;
	}

	public void acClientSelect(SelectEvent event) {
		DAOReserve daoReserve = new DAOReserve();
		DAOProduct daoProduct = new DAOProduct();
		this.reservedProductList = new ArrayList<Product>();
		try {
			if (this.client != null){
				this.reserveList = daoReserve.getReserveListByClient(this.client);
				for (Reserve reserve : this.reserveList){
					this.reservedProductList.add(daoProduct.getProductByBarcode(reserve.getBarcode()));
				}
			}
		} catch (Exception e) {
			FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, "Erro", e.getMessage());
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		}
	}
	
	public void onRegisterClick(ActionEvent actionEvent){
		try {
			Reserve aux = new Reserve();
			aux.setBarcode(this.product.getBarcode());
			aux.setClient(this.client);
			DAOReserve daoReserve = new DAOReserve();
			daoReserve.insert(aux);
			daoReserve = null;
		} catch (Exception e){
			FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, "Erro", e.getMessage());
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		}
	}
	
	public void onDeleteClick(ActionEvent actionEvent) {
		try {
			if (this.reservedProduct.getBarcode().equals("")){
				throw new Exception("É necessário selecionar uma reserva antes de executar esta operação!");
			}
			Reserve aux = null;
			for (Reserve reserve : this.getReserveList()){
				if (reserve.getBarcode().equals(this.reservedProduct.getBarcode())){
					aux = reserve;
					break;
				}
			}
			if (aux == null){
				throw new Exception("Operação não permitida");
			}
			DAOReserve daoReserve = new DAOReserve();
			daoReserve.delete(aux);
			daoReserve = null;
		} catch (DatabaseException ex){
			FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Erro ao excluir", "Verifique se o gênero selecionado não possui produtos vinculados!");
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		} catch (SQLException ex) {
			FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, "Erro na base de dados", ex.getMessage());
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		} catch (InvalidGenreException ex){
			FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, "Gênero inválido", ex.getMessage());
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		} catch (Exception ex) {
			FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, "Erro desconhecido", ex.getMessage());
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		}
	}

}
